<template>
  <div id="allmap"></div>
</template>
<style scoped>
  #allmap{
    height: 100%;
  }
</style>
<script type="text/babel">
 export default {
   data (){
   return {
     map:null,
     BMap:null
   }
  },
   props:{
   },
  methods :{
    /*** 初始化地图 * **/
    init(){
      let BMap = this.BMap = window.BMap;
      let map = this.map= new BMap.Map("allmap");   // 创建Map实例
      let point = new BMap.Point(116.404, 39.915); // 创建点坐标
      map.centerAndZoom(point,12);
      map.addControl(new BMap.MapTypeControl({
        mapTypes:[
          BMAP_NORMAL_MAP,
          BMAP_HYBRID_MAP
        ]}));
      map.addControl(new BMap.NavigationControl());
      map.enableScrollWheelZoom(true);
    },
    addScript() {
      let script = document.createElement("script");
      script.type = "text/javascript";
      script.src = "http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js";
      document.body.appendChild(script);
    },
    oadJScript() {
      let script = document.createElement("script");
      script.type = "text/javascript";
      script.src = "http://api.map.baidu.com/api?v=2.0&ak=GBxFTGWYF0v439Yp40SmjvBL&callback=initMap";
      document.body.appendChild(script);
    },
  },
   mounted(){
     let that=this;
     window.onload = this.oadJScript();  //异步加载地图
     window.initMap=function(){
       that.init();
       if(that.map){
         that.addScript()
         console.log('map')
       }else {
         console.log('no map')
         that.oadJScript();
       }

       require ('@/views/monitor/maplib0')
       that.$emit('initmap',that.map);

       let control = new BMap.ScaleControl();// 左上角，添加比例尺
       that.map.addControl(control);
     }

   }
 }
</script>
